package 力扣日常刷题.木22一月.第05天0113;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/1/13
 * @description
 */
public class Demo03回溯中等全排列 {


    ArrayList<List<Integer>> ans = new ArrayList<>();
    boolean[] isUsed;
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        isUsed = new boolean[nums.length];
        dfs(nums,new ArrayList<Integer>());
        return ans;
    }

    void dfs(int[]nums,ArrayList<Integer> list){
        if(list.size() == nums.length){
            ans.add(new ArrayList<>(list));
            return;
        }
        int last = -10;

        for (int i = 0; i < nums.length; i++) {
            if(!isUsed[i] && last != nums[i]){
                list.add(nums[i]);
                isUsed[i]  = true;
                dfs(nums,list);
                isUsed[i] = false;
                list.remove(list.size()-1);
                last = nums[i];
            }

        }
    }

}
